# A custom ggplot theme
my.theme <- function(base_size = 11, base_family = "",
                     grid.x_colour = NA, grid.y_colour = NA,
                     grid.x_linetype = 1, grid.y_linetype = 1,
                     strip_colour = "grey15", strip_text = "black",
                     background_colour = "transparent",
                     tick_colour = "black",
                     borderless = 0, bordersize = 0.5){ 
  if(is.na(grid.x_colour)) grid.x_colour <- "transparent"
  if(is.na(grid.y_colour)) grid.y_colour <- "transparent"
  if(borderless == 2){
    border <- theme(#axis.line = element_line(colour = "black", size = 0.25`),
      panel.border = element_blank(),
      panel.background = element_blank(),
      strip.background = element_blank())
  }
  
  else if(borderless == 1){
    border <- theme(axis.line = element_line(colour = "black", size = 0.25),
                    panel.border = element_blank(),
                    panel.background = element_blank(),
                    strip.background = element_blank())
  }
  else if(borderless == 0) border <- theme()
  theme( 
    axis.text.x       = element_text(family = base_family, colour = "grey15", size = base_size, vjust = 1, lineheight = 0.9),
    axis.text.y       = element_text(family = base_family, colour = "grey15", size = base_size, hjust = 1, lineheight = 0.9),
    axis.ticks        = element_line(colour = tick_colour, size = 0.2),
    axis.ticks.length = unit(0.2, "lines"),
    # axis.ticks.margin = unit(0.12, "cm"),
    axis.title.x      = element_text(family = base_family, size = base_size, colour = "black", vjust = 0),
    axis.title.y      = element_text(family = base_family, size = base_size, angle = 90, colour = "black", vjust = 1),
    legend.background = element_rect(fill = "grey95"), #element_blank(),
    legend.key = element_blank(),
    legend.key.size = unit(0.6, "lines"),
    legend.text = element_text(family = base_family, size = base_size, face = "plain", lineheight = 1),
    legend.text.align = -1, 
    legend.title = element_blank(), #(family = base_family, size = base_size, face = "plain"),
    legend.title.align = 1,
    legend.position = "top",
    legend.direction = "horizontal",
    legend.justification = c(1, 1),
    panel.background = element_rect(fill = "white"),
    panel.border = element_rect(fill = "transparent", colour = "black", size = bordersize),
    panel.grid.major.x = element_line(colour = grid.x_colour, size = 0.15, linetype = grid.x_linetype),
    panel.grid.major.y = element_line(colour = grid.y_colour, size = 0.15, linetype = grid.y_linetype),
    panel.grid.minor = element_blank(),
    panel.margin = unit(0.75, "lines"),
    strip.background = element_blank(),
    strip.text.x = element_text(family = base_family, size = base_size, face = "bold", colour = strip_text),
    strip.text.y = element_text(family = base_family, size = base_size, face = "plain", angle = -90, colour = strip_text),
    plot.background = element_rect(fill = background_colour, colour = background_colour),
    plot.title = element_text(family = base_family, size = base_size * 1.1, vjust = 0.5, hjust = 0.5, face = "bold")
  ) +
    border
}
